﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class GLPhanBoChiPhiTraTruoc
    {
        private double mDaPhanBo = 0.0;
        private string mKhoaBoPhanCo = "";
        private string mKhoaBoPhanNo = "";
        private string mKhoaChungTuPhanBo = "";
        private string mKhoaDonVi = "";
        private string mKhoaHopDongCo = "";
        private string mKhoaHopDongNo = "";
        private string mKhoaSanPhamCo = "";
        private string mKhoaSanPhamNo = "";
        private string mKhoaTaiKhoanCo = "";
        private string mKhoaTaiKhoanNo = "";
        private string mLoaiTien = "";
        private string mNamThang = "";
        private double mSoConLai = 0.0;
        private double mSoPhanBo = 0.0;
        private double mSoPhaSinh = 0.0;
        private double mTyGia = 0.0;

        public bool ChuaPhanBo(string strNamThang)
        {
            string commandText = " SELECT TOP 1 Khoa FROM GL_DinhNghiaPhanBoChiPHiTraTruoc WHERE ThangBatDauPhanBo <= '" + strNamThang.Trim() + "' AND ConLai > 0 ";
            if (Conversions.ToString(LVYModule.ReturnIfNull(RuntimeHelpers.GetObjectValue(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), "", modGeneral.TypeOfValue.Nothing)).Trim() == "")
            {
                return false;
            }
            commandText = " SELECT TOP 1 NamThang FROM GL_QuaTrinhPhanBoChiPhiTraTruoc WHERE NamThang = '" + strNamThang.Trim() + "'";
            return (Conversions.ToString(LVYModule.ReturnIfNull(RuntimeHelpers.GetObjectValue(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), "", modGeneral.TypeOfValue.Nothing)).Trim() == "");
        }

        public void ClearPhanBo(string strNamThang)
        {
            string commandText = " DELETE FROM GL_QuaTrinhPhanBoChiPhiTraTruoc WHERE NamThang = '" + strNamThang.Trim() + "'";
            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
        }

        public bool DaPhanBoChiPhi(string strNamThang)
        {
            string commandText = " SELECT TOP 1 NamThang FROM GL_QuaTrinhPhanBoChiPhiTraTruoc WHERE NamThang = '" + strNamThang.Trim() + "'";
            return (Conversions.ToString(LVYModule.ReturnIfNull(RuntimeHelpers.GetObjectValue(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), "", modGeneral.TypeOfValue.Nothing)).Trim() != "");
        }

        public DataTable GetDataPhanBo(string StrNamThang, string strKhoaDonVi)
        {
            DataTable table;
            try
            {
                string commandText = " SELECT TOP 1 NamThang FROM GL_QuaTrinhPhanBoChiPhiTraTruoc WHERE NamThang = '" + StrNamThang.Trim() + "'";
                if (Conversions.ToString(LVYModule.ReturnIfNull(RuntimeHelpers.GetObjectValue(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), "", modGeneral.TypeOfValue.Nothing)).Trim() != "")
                {
                    commandText = " SELECT PB.KhoaChungTuPhanBo as Khoa, PB.KhoaTaiKhoanCo, TKC.SoTaiKhoan as SoTaiKhoanCo, DN.SoChungTu, dbo.Char2Date(DN.NgayChungTu) as Ngay,  PB.SoPhatSinh, PB.SoDaPhanBo as DaPhanBo, PB.SoPhanBo, PB.SoConLai, PB.KhoaTaiKhoanNo, TKN.SoTaiKhoan as SoTaiKhoanNo,  PB.KhoaBoPhanNo, BPN.Ten" + modGeneral.H_LANGUAGE + " as BoPhanNo, PB.KhoaSanPhamNo, SPN.Ten" + modGeneral.H_LANGUAGE + " as SanPhamNo, PB.KhoaHopDongNo, HDN.SoHopDong as  HopDongNo, PB.KhoaBoPhanCo, BPC.Ten" + modGeneral.H_LANGUAGE + " as BoPhanCo, PB.KhoaSanPhamCo, SPC.Ten" + modGeneral.H_LANGUAGE + " as SanPhamCo, PB.KhoaHopDongCo, HDC.SoHopDong as HopDongCo FROM GL_QuaTrinhPhanBoChiPhiTraTruoc PB  LEFT JOIN GL_DinhNghiaPhanBoChiPhiTraTruoc DN ON PB.KhoaChungTuPhanBo = DN.Khoa  LEFT JOIN DM_TaiKhoan TKN on PB.KhoaTaiKhoanNo = TKN.Khoa  LEFT JOIN DM_DonViBoPhan BPN on PB.KhoaBoPhanNo = BPN.Khoa  LEFT JOIN DM_SanPhamCongViec SPN on PB.KhoaBoPhanNo = SPN.Khoa  LEFT JOIN DM_HopDong HDN on PB.KhoaHopDongNo = HDN.Khoa  LEFT JOIN DM_TaiKhoan TKC on PB.KhoaTaiKhoanCo = TKC.Khoa  LEFT JOIN DM_DonViBoPhan BPC on PB.KhoaBoPhanCo = BPC.Khoa  LEFT JOIN DM_SanPhamCongViec SPC on PB.KhoaBoPhanCo = SPC.Khoa  LEFT JOIN DM_HopDong HDC on PB.KhoaHopDongCo = HDC.Khoa  WHERE PB.NamThang = '" + StrNamThang.Trim() + "' AND PB.KhoaDonVi = '" + strKhoaDonVi.Trim() + "'";
                }
                else
                {
                    commandText = " SELECT PB.Khoa , PB.KhoaTaiKhoan as KhoaTaiKhoanCo, TKC.SoTaiKhoan as SoTaiKhoanCo, PB.SoChungTu, dbo.Char2Date(PB.NgayChungTu) as Ngay,  PB.SoPhatSinh, PB.DaPhanBo , PB.TriGiaPhanBo as SoPhanBo, PB.ConLai as SoConLai, PB.KhoaTaiKhoanNo, TKN.SoTaiKhoan as SoTaiKhoanNo,  PB.KhoaBoPhanNo, BPN.Ten" + modGeneral.H_LANGUAGE + " as BoPhanNo, PB.KhoaSanPhamNo, SPN.Ten" + modGeneral.H_LANGUAGE + " as SanPhamNo, PB.KhoaHopDongNo, HDN.SoHopDong as  HopDongNo, PB.KhoaBoPhanCo, BPC.Ten" + modGeneral.H_LANGUAGE + " as BoPhanCo, PB.KhoaSanPhamCo, SPC.Ten" + modGeneral.H_LANGUAGE + " as SanPhamCo, PB.KhoaHopDongCo, HDC.SoHopDong as HopDongCo FROM GL_DinhNghiaPhanBoChiPhiTraTruoc PB  LEFT JOIN DM_TaiKhoan TKN on PB.KhoaTaiKhoanNo = TKN.Khoa  LEFT JOIN DM_DonViBoPhan BPN on PB.KhoaBoPhanNo = BPN.Khoa  LEFT JOIN DM_SanPhamCongViec SPN on PB.KhoaBoPhanNo = SPN.Khoa  LEFT JOIN DM_HopDong HDN on PB.KhoaHopDongNo = HDN.Khoa  LEFT JOIN DM_TaiKhoan TKC on PB.KhoaTaiKhoan = TKC.Khoa  LEFT JOIN DM_DonViBoPhan BPC on PB.KhoaBoPhanCo = BPC.Khoa  LEFT JOIN DM_SanPhamCongViec SPC on PB.KhoaBoPhanCo = SPC.Khoa  LEFT JOIN DM_HopDong HDC on PB.KhoaHopDongCo = HDC.Khoa  WHERE PB.NgungPhanBo = 0 And PB.ConLai > 0 and PB.ThangBatDauPhanBo <= '" + StrNamThang + "'";
                }
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool GhiSoPhanBoChiPhi(string pKhoaChungTu, string pSoChungTu, string pNamThang, string pAction, string pNgayChungTu, string pDienGiai)
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[8];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@KhoaChungTu", DbType.Int64, ParameterDirection.Input, pKhoaChungTu);
                commandParameters[1] = DBHelper.createParameter("@SoChungTu", DbType.Int64, ParameterDirection.Input, pSoChungTu);
                commandParameters[2] = DBHelper.createParameter("@NamThang", DbType.Int64, ParameterDirection.Input, pNamThang);
                commandParameters[3] = DBHelper.createParameter("@Action", DbType.Int64, ParameterDirection.Input, pAction);
                commandParameters[4] = DBHelper.createParameter("@KhoaDonVi", DbType.Int64, ParameterDirection.Input, modGeneral.H_CLIENT);
                commandParameters[5] = DBHelper.createParameter("@NgayChungTu", DbType.Int64, ParameterDirection.Input, pNgayChungTu);
                commandParameters[6] = DBHelper.createParameter("@DienGiai", DbType.Int64, ParameterDirection.Input, pDienGiai);
                commandParameters[7] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_GhiSoFromPhanBoChiPhi", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[7].Value, 0, false))
                {
                    return false;
                }
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[0x11];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@NamThang", DbType.Int64, ParameterDirection.Input, this.mNamThang);
                commandParameters[1] = DBHelper.createParameter("@KhoaChungTuPhanBo", DbType.Int64, ParameterDirection.Input, this.mKhoaChungTuPhanBo);
                commandParameters[2] = DBHelper.createParameter("@KhoaTaiKhoanNo", DbType.Int64, ParameterDirection.Input, this.mKhoaTaiKhoanNo);
                commandParameters[3] = DBHelper.createParameter("@KhoaBoPhanNo", DbType.Int64, ParameterDirection.Input, this.mKhoaBoPhanNo);
                commandParameters[4] = DBHelper.createParameter("@KhoaSanPhamNo", DbType.Int64, ParameterDirection.Input, this.mKhoaSanPhamNo);
                commandParameters[5] = DBHelper.createParameter("@KhoaHopDongNo", DbType.Int64, ParameterDirection.Input, this.mKhoaHopDongNo);
                commandParameters[6] = DBHelper.createParameter("@KhoaTaiKhoanCo", DbType.Int64, ParameterDirection.Input, this.mKhoaTaiKhoanCo);
                commandParameters[7] = DBHelper.createParameter("@KhoaBoPhanCo", DbType.Int64, ParameterDirection.Input, this.mKhoaBoPhanCo);
                commandParameters[8] = DBHelper.createParameter("@KhoaSanPhamCo", DbType.Int64, ParameterDirection.Input, this.mKhoaSanPhamCo);
                commandParameters[9] = DBHelper.createParameter("@KhoaHopDongCo", DbType.Int64, ParameterDirection.Input, this.mKhoaHopDongCo);
                commandParameters[10] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[11] = DBHelper.createParameter("@TyGia", DbType.Date, ParameterDirection.Input, this.mTyGia);
                commandParameters[12] = DBHelper.createParameter("@SoPhaSinh", DbType.Date, ParameterDirection.Input, this.mSoPhaSinh);
                commandParameters[13] = DBHelper.createParameter("@DaPhanBo", DbType.Date, ParameterDirection.Input, this.mDaPhanBo);
                commandParameters[14] = DBHelper.createParameter("@SoPhanBo", DbType.Date, ParameterDirection.Input, this.mSoPhanBo);
                commandParameters[15] = DBHelper.createParameter("@SoConLai", DbType.Date, ParameterDirection.Input, this.mSoConLai);
                commandParameters[0x10] = DBHelper.createParameter("@KhoaDonVi", DbType.Int64, ParameterDirection.Input, this.mKhoaDonVi);
                flag = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "GL_sp_QuaTrinhPhanBoChiPhiTraTruoc", commandParameters) > 0;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public double DaPhanBo
        {
            get
            {
                return this.mDaPhanBo;
            }
            set
            {
                this.mDaPhanBo = value;
            }
        }

        public string KhoaBoPhanCo
        {
            get
            {
                return this.mKhoaBoPhanCo;
            }
            set
            {
                this.mKhoaBoPhanCo = value;
            }
        }

        public string KhoaBoPhanNo
        {
            get
            {
                return this.mKhoaBoPhanNo;
            }
            set
            {
                this.mKhoaBoPhanNo = value;
            }
        }

        public string KhoaChungTuPhanBo
        {
            get
            {
                return this.mKhoaChungTuPhanBo;
            }
            set
            {
                this.mKhoaChungTuPhanBo = value;
            }
        }

        public string KhoaDonVi
        {
            get
            {
                return this.mKhoaDonVi;
            }
            set
            {
                this.mKhoaDonVi = value;
            }
        }

        public string KhoaHopDongCo
        {
            get
            {
                return this.mKhoaHopDongCo;
            }
            set
            {
                this.mKhoaHopDongCo = value;
            }
        }

        public string KhoaHopDongNo
        {
            get
            {
                return this.mKhoaHopDongNo;
            }
            set
            {
                this.mKhoaHopDongNo = value;
            }
        }

        public string KhoaSanPhamCo
        {
            get
            {
                return this.mKhoaSanPhamCo;
            }
            set
            {
                this.mKhoaSanPhamCo = value;
            }
        }

        public string KhoaSanPhamNo
        {
            get
            {
                return this.mKhoaSanPhamNo;
            }
            set
            {
                this.mKhoaSanPhamNo = value;
            }
        }

        public string KhoaTaiKhoanCo
        {
            get
            {
                return this.mKhoaTaiKhoanCo;
            }
            set
            {
                this.mKhoaTaiKhoanCo = value;
            }
        }

        public string KhoaTaiKhoanNo
        {
            get
            {
                return this.mKhoaTaiKhoanNo;
            }
            set
            {
                this.mKhoaTaiKhoanNo = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NamThang
        {
            get
            {
                return this.mNamThang;
            }
            set
            {
                this.mNamThang = value;
            }
        }

        public double SoConLai
        {
            get
            {
                return this.mSoConLai;
            }
            set
            {
                this.mSoConLai = value;
            }
        }

        public double SoPhanBo
        {
            get
            {
                return this.mSoPhanBo;
            }
            set
            {
                this.mSoPhanBo = value;
            }
        }

        public double SoPhaSinh
        {
            get
            {
                return this.mSoPhaSinh;
            }
            set
            {
                this.mSoPhaSinh = value;
            }
        }

        public double TyGia
        {
            get
            {
                return this.mTyGia;
            }
            set
            {
                this.mTyGia = value;
            }
        }
    }
}

